package com.lishe.lishe.lishe.module.excitation.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lishe.lishe.Imageloader.ImageLoaderUtils;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.module.excitation.adapter.ViewpagerAdapter;
import com.lishe.lishe.lishe.module.excitation.bean.HeadInfo;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by shino on 2017/11/23.
 */

public class ViewpagerDialog extends Dialog {

    public ViewpagerDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }


    public static class Builder implements View.OnClickListener {
        private Context context;
        private List<HeadInfo> users = new ArrayList<>();

        public Builder(Context context) {
            this.context = context;
        }

        public void setData(List<HeadInfo> users) {
            this.users.clear();
            this.users.addAll(users);
            create();
        }

        private ViewpagerDialog dialog;
        private ViewpagerAdapter adapter;
        private ViewPager viewPager;

        private int oldIndex = 0;

        public ViewpagerDialog create() {
            dialog = new ViewpagerDialog(context, R.style.ViewPagerDialog);
            View view = LayoutInflater.from(context).inflate(R.layout.dialog_user_info, null);
            LinearLayout pointLayout = view.findViewById(R.id.pointLayout);
            pointLayout.removeAllViews();
            points.clear();
            //小圆点
            for (int i = 0; i < users.size(); i++) {
                View pointView = LayoutInflater.from(context).inflate(R.layout.item_carousel_point, null);
                View point = pointView.findViewById(R.id.point);
                if (users.size() <= 1) {
                    pointView.setVisibility(View.INVISIBLE);
                } else {
                    pointView.setVisibility(View.VISIBLE);
                }
                pointLayout.addView(pointView);
                points.add(point);
            }
            ImageView cancle = view.findViewById(R.id.cancle);
            viewPager = view.findViewById(R.id.viewPager);
            viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                }

                @Override
                public void onPageSelected(int position) {
                    if (points.size() > oldIndex) {
                        points.get(oldIndex).setBackgroundResource(R.drawable.shape_coner50px);
                    }
                    if (points.size() > position) {
                        points.get(position).setBackgroundResource(R.drawable.shape_red50px);
                    }
                    oldIndex = position;
                }

                @Override
                public void onPageScrollStateChanged(int state) {
                }
            });
            if (adapter == null) {
                adapter = new ViewpagerAdapter(context, users);
            }
            viewPager.setAdapter(adapter);
            adapter.notifyDataSetChanged();
            cancle.setOnClickListener(this);
            dialog.setContentView(view);
            viewPager.setCurrentItem(oldIndex);
            WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
            params.width = WindowManager.LayoutParams.MATCH_PARENT;
            params.height = WindowManager.LayoutParams.MATCH_PARENT;
            dialog.getWindow().setAttributes(params);
            return dialog;
        }

        public void show() {
            if (dialog == null) {
                create();
            }
            dialog.show();
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.cancle:
                    dialog.dismiss();
                    break;
            }
        }

        /**
         * 显示指定页
         *
         * @param page
         */
        public void showPage(int page) {
            if (page < users.size()) {
                oldIndex = page;
                viewPager.setCurrentItem(page);
                points.get(page).setBackgroundResource(R.drawable.shape_red50px);
            }
        }

        private LinkedList<View> points = new LinkedList<>();//轮播的图片的小圆点
    }
}
